﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ClassLibrary.Entity;
using ClassLibrary.DAO;
namespace mprsc
{
    public partial class EDITQUOTE : Form
    {
        TblQuoteDAO quoteDAO = new TblQuoteDAO(Global.CONNECTION_STRING);
        TblJobsDAO JobDAO = new TblJobsDAO(Global.CONNECTION_STRING);
        TblStatusJobDAO StatusJobDAO = new TblStatusJobDAO(Global.CONNECTION_STRING);

        public delegate void SendMessage(string Message);
        public SendMessage Sender; 

        public EDITQUOTE()
        {
            InitializeComponent();
            //Tao con tro toi hàm GetMessage
            Sender = new SendMessage(GetMessage);
            LoadStatus();

            if (rdAmount.Checked)
            {
                txtAmountQuoted.Enabled = true;
            }
            if (rdInspectionFee.Checked)
            {
                txtInspection.Enabled = true;
            }
           
        }

        //Hàm có nhiem vu lay tham so truyen vào
        private void GetMessage(string Message)
        {
            lbJob.Text = Message;
        }
        private void EDITQUOTE_Load(object sender, EventArgs e)
        {
            this.CenterToParent();
            if (lbJob.Text != "")
            {
                loadQuote();
            }
            
        }

        // load status
        private void LoadStatus()
        {
            var status = new Dictionary<int, string>();
            status.Add(1, "Waiting for quote");
            status.Add(2, "Quoted");
            status.Add(3, "Quote Accepted");
            status.Add(4, "Quote Rejected");
            status.Add(5, "BER");
            cbStatus.DataSource = new BindingSource(status, null);
            cbStatus.DisplayMember = "Value";
            cbStatus.ValueMember = "Value";
        }
        private void loadQuote()
        {
            string id = lbJob.Text;

            try
            {
                if (id != null)
                {
                    DataTable dtQuote = quoteDAO.GetQuote(Convert.ToInt32(id));
                    foreach (DataRow dr in dtQuote.Rows)
                    {
                        txtQuoteID.Text = dr["QuoteID"].ToString();
                        if (Convert.ToInt32(dr["InspectionFee"]) != 0)
                            rdInspectionFee.Checked = true;
                        else
                            rdAmount.Checked = true;
                    }

                    grdQuote.DataSource = dtQuote;
                   
                }
                else
                {
                    txtInspection.Text = string.Empty;
                    txtAmountQuoted.Text = string.Empty;
                    txtDate.Text = DateTime.Now.ToString("dd/MM/yyyy");
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            bool flag = false;
            int id = Global.ConvertToInt(lbJob.Text);
            if (txtAmountQuoted.Text != "" || txtQuoteID.Text != "")
            {
                try
                {

                    TblQuote tblQuote = new TblQuote();
                    tblQuote.JobID = id;
                    if (rdInspectionFee.Checked == true)
                    {
                        tblQuote.InspectionFee = Convert.ToDecimal(txtInspection.Text);
                        tblQuote.AmountQuoted = 0;
                    }
                    if (rdAmount.Checked == true)
                    {
                        tblQuote.InspectionFee = 0;
                        tblQuote.AmountQuoted = Convert.ToDecimal(txtAmountQuoted.Text);
                    }

                    tblQuote.DateQuoted = DateTime.Now;
                    tblQuote.QuoteStatus = cbStatus.Text;
                    tblQuote.Notes = txt_Notes.Text;
                    tblQuote.OutCome = "";

                    if (txtQuoteID.Text == "")
                    {
                        quoteDAO.InsTblQuote(tblQuote, 1);
                    }
                    else
                    {
                        tblQuote.QuoteID = Convert.ToInt32(txtQuoteID.Text);
                        quoteDAO.InsTblQuote(tblQuote, 2);
                    }
                    TblJobs Jobs = new TblJobs();
                    Jobs.JobID = id;
                    if (cbStatus.SelectedValue.ToString() == "1")
                    {
                        Jobs.QuotationStatus = 1;
                    }
                    if (cbStatus.SelectedValue.ToString() == "2")
                    {
                        Jobs.QuotationStatus = 2;
                    }
                    if (cbStatus.SelectedValue.ToString() == "3")
                    {
                        Jobs.QuotationStatus = 3;
                    }
                    if (cbStatus.SelectedValue.ToString() == "4")
                    {
                        Jobs.QuotationStatus = 4;
                    }

                    JobDAO.UpdateQuoted(Jobs.QuotationStatus, id);

                    TblJobStatus JobStatus = new TblJobStatus();
                    JobStatus.JobID = id;
                    JobStatus.StatusDate = DateTime.Now;
                    JobStatus.StatusJobs = tblQuote.QuoteStatus;
                    JobStatus.Notes = tblQuote.Notes;

                    StatusJobDAO.JobStatusIns(JobStatus);

                    flag = true;

                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            if (flag)
            {
                loadQuote();
                string idJob = lbJob.Text;
                this.Hide();
                NewBooking booking = new NewBooking();
                booking.Sender(idJob);
                booking.Show();
            }
        }

        private void rdAmount_CheckedChanged(object sender, EventArgs e)
        {
            txtAmountQuoted.Enabled = true;
            txtInspection.Enabled = false;
        }

        private void rdInspectionFee_CheckedChanged(object sender, EventArgs e)
        {
            txtAmountQuoted.Enabled = false;
            txtInspection.Enabled = true;
        }
    }
}
